<?php
namespace app\api\controller\notify;

use app\api\service\DeliveryService;
use think\facade\Request;
use think\facade\Log;
use think\facade\Db;

// 订单相关回调
class OrderNotify
{

    // 订单状态变化回调
    public function statusNotify(Request $request)
    {
        $params = Request::post();
        \think\facade\Log::info('订单状态变化回调---:' . json_encode($params));
        $params['app_type'] = $this->getAppType($params);
        if (empty($params['app_type'])){
            output_error('未获取到配送平台类型');
        }

        $service = new DeliveryService(get_app_type_str($params['app_type']));
        $result = $service->statusNotify($params);

        return $result;
    }

    // 根据订单信息获取配送平台类型
    public function getAppType($params)
    {
        $app_type = 0;
        if (!empty($params['orderNo'])){
            $where['call_number'] =$params['orderNo'];
            if (!empty($params['issOrderNo'])){
                $where['third_number'] =$params['issOrderNo'];
            }
        }elseif (!empty($params['order_id'])){
            $where[] = ['call_number', '=', $params['order_id']];
            $where[] = ['status', '<>', 0];
        }
        if (!empty($where)){
            $order = Db::name('order')->where($where)->order('id','desc')->find();
            if (!empty($order['app_type'])){
                $app_type = $order['app_type'];
            }
        }
        return $app_type;
    }
}
?>